<?php
namespace Admin\Controller;
use Admin\Controller\AdminController;
class ArticleController extends AdminController {
	public function index(){
		$title = I('title');
		$where = array();
		$p     = I('p','1');
		if(!empty($title)){
			$where['title'] = $title;
		}
		$list = M('Article as article')->join('base_article_type as type on article.class = type.class')->where($where)->order('sort')->page($p.',10')->select();
        $allcount = M('Article as article')->join('base_article_type as type on article.class = type.class')->where($where)->count();
        $Page       = new \Think\Page($allcount,10);
        $Page ->parameter = $where;
        $show       = $Page->show();
        $this -> assign('list',$list);
        $this -> assign('show',$show);
		$this -> display();
	}
	public function add(){
		$articletype=M('ArticleType')->select();
		$this -> assign('type',$articletype);
		$this -> display();
	}
	public function insert(){
		$title   = I('title','');
		$class = I('class','');
		$sort    = I('sort','');
		$c_summary = I('c_summary','');
		$c_content = isset($_POST['c_content'])?$_POST['c_content']:'';
		$status    = I('status',1);
	    ////可选
	    $auther = I('auther');
	    $key    = I('keyword');
	    $top    = I('top',2);
	    $e_summary = I('e_summary');
	    $e_content = isset($_POST['e_content'])?$_POST['e_content']:'';
	    if(empty($title)){
	    	$this ->error('标题不能为空');
	    }
	    if(empty($_FILES['pic']['name'])){
	    	$this -> error('首图不能为空');
	    }
	    $pic = $this->upload($_FILES['pic']);
	    if(empty($class)){
	    	$this ->error('分类不能为空');
	    }
	    if(empty($sort)){
	    	$this ->error('排序不能为空');
	    }
	    if(empty($c_summary)){
	    	$this ->error('中文摘要不能为空');
	    }
	    if(empty($c_content)){
	    	$this ->error('中文内容不能为空');
	    }
	    $data['title'] = $title;
	    $data['class'] = $class;
	    $data['sort'] = $sort;
	    $data['c_summary'] = $c_summary;
	    $data['c_content'] = $c_content;
	    $data['status'] = $status;
	    $data['auther'] = $auther;
	    $data['key'] = $key;
	    $data['top'] = $top;
	    $data['e_summary'] = $e_summary;
	    $data['e_content'] = $e_content;
	    $data['add_time'] = time();
	    $data['pic']      = $pic;
	    if(!M('Article')->add($data)){
	    	$this -> error('系统繁忙');
	    }
	    $this -> success('添加成功','index');
	}
	public function del(){
		$article_id = I('article_id');
		if(empty($article_id)){
			 $this -> error('系统繁忙');
		}
		$where['article_id'] = $article_id;
		if(!M('Article')->where($where)->delete()){
               $this -> error('系统繁忙');
		}
		$this -> success('成功删除');
	}
	public function edit(){
		$article_id = I('article_id');
		$where['article_id'] = $article_id;
		$row = M('Article')->where($where)->find();
		$articletype=M('ArticleType')->select();
		$this -> assign('type',$articletype);
		$this -> assign('row',$row);
		$this -> display();
	}
	public function update(){
		$article_id = I('article_id');
		if(empty($article_id)){
			$this -> error('系统繁忙');
		}
		$title   = I('title','');
		$class = I('class','');
		$sort    = I('sort','');
		$c_summary = I('c_summary','');
		$c_content = $_POST['c_content'];
		$status    = I('status',1);
	    ////可选
	    $auther = I('auther');
	    $key    = I('keyword');
	    $top    = I('top',2);
	    $pic    = I('pic');
	    $e_summary = I('e_summary');
	    $e_content = $_POST['e_content'];
	    if(empty($title)){
	    	$this ->error('标题不能为空');
	    }
	    if(!empty($_FILES['file']['name'])){
	    	$pic = $this->upload($_FILES['file']);
	    }
	    if(empty($class)){
	    	$this ->error('分类不能为空');
	    }
	    if(empty($sort)){
	    	$this ->error('排序不能为空');
	    }
	    if(empty($c_summary)){
	    	$this ->error('中文摘要不能为空');
	    }
	    if(empty($c_content)){
	    	$this ->error('中文内容不能为空');
	    }
	    $data['title'] = $title;
	    $data['class'] = $class;
	    $data['sort'] = $sort;
	    $data['c_summary'] = $c_summary;
	    $data['c_content'] = $c_content;
	    $data['status'] = $status;
	    $data['auther'] = $auther;
	    $data['key'] = $key;
	    $data['top'] = $top;
	    $data['e_summary'] = $e_summary;
	    $data['e_content'] = $e_content;
	    $data['pic']      = $pic;
	    if(!M('Article')->where(array('article_id'=>$article_id))->save($data)){
	    	$this -> error('系统繁忙');
	    }
	    $this -> success('修改成功');
	}
}